package model.dba;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Area;

public class AreaDAO extends JDBC {
	private static AreaDAO areaDAO;

	private AreaDAO() {
		super();
	}

	public static AreaDAO getInstance() {
		if (areaDAO == null) {
			areaDAO = new AreaDAO();
		}
		return areaDAO;
	}

	public int add(Area area) {
		try {
			String queryString = "INSERT INTO areas(nombre)" + " VALUES(?) ";
			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setString(1, area.getNombre());

			preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return getLastId();

	}

	public boolean update(Area area) {
		boolean state = false;
		try {
			String queryString = "UPDATE areas SET nombre=?, WHERE idArea=?";

			preparedStatement = connection.prepareStatement(queryString);

			preparedStatement.setString(1, area.getNombre());
			preparedStatement.setLong(2, area.getId());

			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			}

			catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return state;
	}

	public boolean delete(Integer id) {
		boolean state = false;
		try {
			String queryString = "DELETE FROM areas WHERE idArea=?";

			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setInt(1, id);
			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return state;

	}

	public Area get(Integer id) {

		Area area = new Area();
		try {
			String queryString = "SELECT * FROM areas where idArea = '" + id + "';";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				area = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return area;
	}

	public Area find(String nombre) {

		Area area = new Area();
		try {
			String queryString = "SELECT * FROM areas where nombre ='" + nombre + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				area = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return area;
	}

	public int getLastId() {
		int ret = 0;
		try {
			String queryString = "SELECT idArea FROM areas order by idArea DESC LIMIT 1  ";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ret = resultSet.getInt("idArea");

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return ret;

	}

	public List<Area> getAll() {
		List<Area> areaList = new ArrayList<Area>();

		try {
			String queryString = "SELECT * FROM areas ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				areaList.add(populate());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return areaList;
	}

	private Area populate() throws SQLException {

		Area area = new Area();
		area.setId((int) resultSet.getLong("idArea"));
		area.setNombre(resultSet.getString("nombre"));
		return area;

	}
}